package com.runchen.broadcastdetection;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.multidex.MultiDex;

import com.blankj.utilcode.util.Utils;
import com.hjq.permissions.XXPermissions;
import com.lxj.xpopup.XPopup;
import com.ruochen.common.customer.DynamicTimeFormat;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshInitializer;

import java.util.List;

public class AppApplication extends Application {
    private static AppApplication instances;

    public static AppApplication getInstances() {
        return instances;
    }

    static {
        //启用矢量图兼容
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        //设置全局默认配置（优先级最低，会被其他设置覆盖）
        SmartRefreshLayout.setDefaultRefreshInitializer(new DefaultRefreshInitializer() {
            @Override
            public void initialize(@NonNull Context context, @NonNull RefreshLayout layout) {
                //全局设置（优先级最低）
                layout.setEnableAutoLoadMore(true);
                layout.setEnableOverScrollDrag(true);
                layout.setEnableOverScrollBounce(true);
                layout.setDisableContentWhenLoading(false);
                layout.setDisableContentWhenRefresh(false);
                layout.setEnableLoadMoreWhenContentNotFull(false);
                layout.setEnableFooterFollowWhenNoMoreData(true);
                layout.setEnableScrollContentWhenRefreshed(true);
                layout.setEnableScrollContentWhenLoaded(true);
                layout.setEnableFooterTranslationContent(true);
                layout.setEnableHeaderTranslationContent(true);
                layout.setPrimaryColorsId(R.color.colorTransparent, R.color.color333333,
                        R.color.color3F3F3F);
                layout.setHeaderMaxDragRate(4.0f);
                layout.setHeaderHeight(50);
                layout.setFooterMaxDragRate(4.0F);
                layout.setFooterHeight(45);
            }
        });
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context,
                                                     @NonNull RefreshLayout layout) {
                //全局设置主题颜色（优先级第二低，可以覆盖 DefaultRefreshInitializer 的配置，与下面的ClassicsHeader绑定）
                return new ClassicsHeader(context).setTimeFormat(new DynamicTimeFormat("更新于 %s"));
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context,
                                                     @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context);
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        String processName = getProcessName(this, android.os.Process.myPid());
        if (processName != null) {
            boolean defaultProcess = getPackageName().equals(processName);
            if (defaultProcess) {
                instances = this;
                Utils.init(this);
                //
                XPopup.setAnimationDuration(300);
                XXPermissions.setScopedStorage(true);
            }
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    public static String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }
}
